//
//  GuestImageViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/8.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "GuestImageViewController.h"

@interface GuestImageViewController ()

@property (nonatomic, strong) UIView *layerView;

@end

@implementation GuestImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 添加layer图
    self.layerView = [[UIView alloc] initWithFrame:CGRectMake(30, 20, 300, 300)];
    self.layerView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
    [self.view addSubview:_layerView];
//    self.layerView.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"图片一"].CGImage);
    self.layerView.layer.contents = (__bridge id _Nullable)([UIImage imageWithName:@"taylorswift"].CGImage);
    // 设置contentsScale,这个值在使用layer的图片时一定要设置，否则在Retina屏幕上就显示不正确了
    self.layerView.layer.contentsScale = [UIScreen mainScreen].scale;
    // 更改图片以适应原比例
//    [self adjustOriginSize];
    // maskToBounds
//    [self userMaskToBounds];
    //  图片区域显示，contentsRect
    [self userContentsRect];
    // contentsCenter
 //   [self userContentsCenter];
}

// 更改图片以适应原比例
- (void)adjustOriginSize
{
  //  self.layerView.contentMode = UIViewContentModeScaleAspectFit;
    /** contentsGravity 可选的变量为
     *  kCAGravityCenter
     kCAGravityTop
     kCAGravityBottom
     kCAGravityLeft
     kCAGravityRight
     kCAGravityTopLeft
     kCAGravityTopRight
     kCAGravityBottomLeft
     kCAGravityBottomRight
     kCAGravityResize
     kCAGravityResizeAspect
     kCAGravityResizeAspectFill
     *
     *
     */
    self.layerView.layer.contentsGravity = kCAGravityResizeAspect;
    
    
// 以上的两句话作用是相同的
}

// maskToBounds
- (void)userMaskToBounds
{
    self.layerView.frame = CGRectMake(30, 20, 200, 200);
    self.layerView.layer.contentsGravity = kCAGravityCenter; // kCAGravityCenter会适当压缩图片，有可能超过图层的范围
    self.layerView.layer.masksToBounds = YES;
    // 补充 ：UIView有一个叫做clipsToBounds的属性可以用来决定是否显示超出边界的内容，CALayer对应的属性叫做masksToBounds，把它设置为YES
    
}

// 图片区域显示，contentsRect
- (void)userContentsRect
{
    self.layerView.layer.contentsRect = CGRectMake(0.5, 0.5, 0.5, 0.5); // 单位坐标0~1
    self.layerView.layer.contentsGravity = kCAGravityResizeAspect;
}

// contentsCenter
- (void)userContentsCenter
{
    self.layerView.layer.contentsCenter = CGRectMake(0.25, 0.25, 0.5, 0.5);
    // 说明：contentsCenter属性只有在图片被拉伸后才会起作用，contentsCenter可以用来定义全面拉伸的范围
 //   self.layerView.layer.contentsGravity = kCAGravityResizeAspect;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
